package com.sec.android.app.myfiles.module.download;

import android.app.DownloadManager;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;

import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.module.download.DownloadFileRecord.DownloadType;
import com.sec.android.app.myfiles.module.local.file.LocalFileOperationImp;
import com.sec.android.app.myfiles.operation.FileOperationException;
import com.sec.android.app.myfiles.operation.ProgressListener;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo.COLUMN_ID;
import com.sec.android.app.myfiles.util.FileDetailUtils;

import java.util.ArrayList;

public class DownloadOperationImp extends LocalFileOperationImp {
    private DownloadManager mDownloadManager;

    public DownloadOperationImp(Context context, ProgressListener listener) {
        super(context, listener);
        try {
            mDownloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
        } catch (NullPointerException e) {
            Log.e(this, "NullPointerException:" + e.toString());
        }
    }

    @Override
    protected boolean isSupport(FileRecord record) {
        boolean bRet = false;

        if (record.getStorageType() == StorageType.Downloads) {
            bRet = true;
        }
        return bRet;
    }

    @Override
    protected boolean isSupportCopyMove(FileRecord src, FileRecord dst) {
        boolean bRet = false;

        if (src != null && dst != null) {
            StorageType srcType = src.getStorageType();
            StorageType dstType = dst.getStorageType();
            if (StorageType.Downloads.equals(srcType) && (StorageType.Local.equals(dstType) || StorageType.Downloads.equals(dstType))) {
                bRet = true;
            }
        }
        return bRet;
    }

    @Override
    protected boolean _delete(FileRecord record) throws FileOperationException {
        boolean bRet = super._delete(record);
        DownloadFileRecord target = (DownloadFileRecord) record;
        mProgresslistener.onProgressChanged(null, 0, 100);

        if (target.getDownloadType() == DownloadType.DOWNLOAD_BY_BROWSER) {
            mDownloadManager.remove(target.getDownloadId());
            bRet = true;
            mProgresslistener.onProgressChanged(null, 50, 100);
        }

        DbTableInfo tableInfo = DownloadDbTableInfo.getInstance();
        String selection = String.format("%s=? AND %s=?", tableInfo.getColumnName(COLUMN_ID.PATH), tableInfo.getColumnName(COLUMN_ID.NAME));
        String[] selectionArgs = new String[]{record.getPath(), record.getName()};
        final Uri uri = Uri.parse(tableInfo.getUri());

        if (mContext.getContentResolver().delete(uri, selection, selectionArgs) > 0) {
            bRet = true;
            mProgresslistener.onProgressChanged(null, 100, 100);
        }
        return bRet;
    }

    @Override
    protected boolean _rename(FileRecord src, FileRecord dst) throws FileOperationException {
        boolean bRet = super._rename(src, dst);
        if (bRet) {
            DbTableInfo tableInfo = DownloadDbTableInfo.getInstance();
            ContentValues value = new ContentValues();
            value.put(tableInfo.getColumnName(COLUMN_ID.NAME), dst.getName());
            value.put(tableInfo.getColumnName(COLUMN_ID.PATH), dst.getPath());
            value.put(tableInfo.getColumnName(COLUMN_ID.FILE_TYPE), dst.getFileType());
            FileDetailUtils.updateDbRecord(mContext, tableInfo, value, src);
        }
        return bRet;
    }

    @Override
    protected FileRecord _mkdir(FileRecord dst, String name) {
        return null;
    }

    @Override
    protected ArrayList<FileRecord> _getListInDirectory(FileRecord record) {
        return null;
    }
}
